﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Collections;

namespace ZebraCoreLib.Framework
{
    public class BizServiceHelper
    {
        public static Hashtable AttrbuteCache = new Hashtable();
        public static Hashtable propertyCache = new Hashtable();

        public static Hashtable bizObjectCache = new Hashtable();
        /// <summary> 
        /// 获取ZebraField属性
        /// </summary>
        /// <param name="bizObject"></param>
        /// <returns></returns>
        public static PropertyInfo[] GetZebraFieldProperties(object bizObject)
        {
            List<PropertyInfo> properties = new List<PropertyInfo>();
            string cacheKey = bizObject.GetType().FullName;
            if (propertyCache[cacheKey] != null)
            {
                properties = propertyCache[cacheKey] as List<PropertyInfo>;
            }
            else
            {
                foreach (var p in bizObject.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
                {
                    if (p.IsDefined(typeof(ZebraFieldAttribute), false))
                        properties.Add(p);
                }
                propertyCache[cacheKey] = properties;
            }
            return properties.ToArray();
        }

        /// <summary>
        /// 获取ZebraField特性
        /// </summary>
        /// <param name="property"></param>
        /// <returns></returns>
        public static ZebraFieldAttribute GetZebraFieldAttribute(PropertyInfo property)
        {
            ZebraFieldAttribute attr = null;
            string cacheKey =property.DeclaringType.FullName + "." + property.Name;

            if(AttrbuteCache[cacheKey]!=null)
            {
                attr = AttrbuteCache[cacheKey] as ZebraFieldAttribute;
            }
            else
            {
                if (property.IsDefined(typeof(ZebraFieldAttribute), false))
                {
                    var attributes= property.GetCustomAttributes(typeof(ZebraFieldAttribute), false);
                    if (attributes != null && attributes.Length >= 0)
                    {
                        attr = attributes[0] as ZebraFieldAttribute;
                        AttrbuteCache[cacheKey] = attr;
                    }
                }
            }

            return attr;
        }
        /// <summary>
        /// 设置ZebraField属性值
        /// </summary>
        /// <param name="bizObject"></param>
        /// <param name="fieldName"></param>
        /// <param name="value"></param>
        public static void SetZebraFieldValue(object bizObject,string fieldName, object value)
        {
            //var properties =GetZebraFieldProperties(bizObject);
            //var p= properties.Where(x => x.Name == fieldName).Single();
            var property = bizObject.GetType().GetProperty(fieldName);
            property.SetValue(bizObject, value, null);
        }

      
        public static void SetFieldValue(object bizObject, string fieldName, object value)
        {
            var field = bizObject.GetType().GetField(fieldName);
            field.SetValue(bizObject, value);
        }


        public static object InvokeBizObjectMethod(object bizObject,string method,object[] parameters)
        {
             MethodInfo m= bizObject.GetType().GetMethod(method);
             return  m.Invoke(bizObject, parameters);
        }


        public static object InvokeBizObjectMethod(object bizObject, string method,Type[] types,object[] parameters)
        {
            MethodInfo m = bizObject.GetType().GetMethod(method, types);
            return m.Invoke(bizObject, parameters);
        }
    }
}
